﻿<script>
    var _sorts = @this.RawSerialize(this.ViewBag.SortList);
    var _sorts2 = new Array();
    var _sts = [{ Title: '取消', Id: 0 }, { Title: '正常', Id:1 }];

    $(_sorts).each(function (i, item) {
        if (item.Pid == "0") {
            _sorts2.push({ "Id": item.Id, "Title": item.Title, "Pid": item.Pid });

            $(_sorts).each(function (ii, item2) {
                if (item2.Pid == item.Id) {
                    _sorts2.push({ "Id": item2.Id, "Title": "     |-----" + item2.Title, "Pid": item2.Pid });
                }
            });
        }
    });

    var _vm;
    $(function () {
        var vm = new PageViewModel();
        _vm = vm;
        ko.applyBindings(vm);
        vm.init();

    });
 

    function PageViewModel()
    {
        var me = this;
        ViewModel.call(me);
        vmExtend.call(me);

        me.url = "/Wiki/Promotion/Models";
        me.deleteUrl = "/Wiki/Promotion/Delete";

        
        me.dialog = new FormDialog(me);
         
    }
    function vmExtend() {
        var me = this;
    }


    function FormDialog(vm) {
        var me = this;
        Dialog.call(me);

        me.onOpen = function ()
        {
            var model = me.editModel();
            if (model)
            {
                var bindModel = $ko.toJS(model);
                me.model(bindModel);

                $ace.get("/Wiki/Promotion/GetPromotionItemList", { PromotionId: bindModel.Id }, function (result) {
                    if (result.Status == 100)
                    {
                        var s = "";
                        result.Data.map(function (item)
                        {
                            s += "<tr id='item_" + item.ProductID + "'>";
                            s += "<td>" + item.ProductID + "</td>";
                            s += "<td>" + item.ProductName + "</td>";
                            s += "<td class='del' data='" + item.ProductID + "'>删除</td>";
                            s += "</tr>";
                        });
                        $("#paylog").html(s);
                    }

                });

            }
            else
            {
                
            }


            $("#btn_add_product").click(function () {

                var id = $("#SelProductID").val();
                var name = $("#SelProductName").val();
                var len = $("#item_" + id).length;
                if (len == 0) {
                    var s = "<tr id='item_" + id + "' class='item'>";
                    s += "<td>" + id + "</td>";
                    s += "<td>" + name + "</td>";
                    s += "<td class='del' data='" + id + "'>删除</td>";
                    s += "</tr>";
                    $("#paylog").append(s);
                }
            });

            $("#ProSortID").change(function () {
                var s = $(this).val();
                if (s != "") {
                    $ace.post("/Wiki/Promotion/GetProductList", { ProSortID: s }, function (result)
                    {
                        if (result.Status == 100)
                        {
                            var shtml = "<option value=''>选择活动产品</option>";
                            result.Data.map(function (item)
                            {
                                shtml += "<option value=\"" + item.Id+"\">" + item.ProductName + "</option>";
                            }); 
                            $("#SelProduct").html(shtml);
                        }
                         
                    });
                }
            });

            $("#SelProduct").change(function ()
            {
                $("#SelProductID").val($("#SelProduct").val());
                $("#SelProductName").val($("#SelProduct").find("option:selected").text());
                //me.model({ ProductID: $("#SelProduct").val(), ProductName: $("#SelProduct").find("option:selected").text()});

            });

            

            $(".del").click(function ()
            {
                var pid = $("#hid_id").val();
                var id = $(this).attr("data");
                if (pid != "" && pid != undefined && pid != null) {
                    $ace.get("/Wiki/Promotion/DeletePromotionItem", { PromotionId: pid, ProductID: id }, function (result) {
                        $ace.msg(result.Msg);
                    });
                }
                else {
                    $(this).parent().remove();
                }
                
            });

        }

        me.onSave = function ()
        {
            if (!$('#form1').formValid())
            {
                return false;
            }

            var arr = new Array();
            $(".item").each(function (i, item) {
                arr.push($(item).children("td").eq(0).text());
            });
            var ids = "";
            if (arr.length > 0) {
                ids = arr.join(';');
            }
            var model = me.model();
            if (me.editModel())
            {
                model.ids = ids;
                $ace.post("/Wiki/Promotion/Edit", model, function (result) {
                    $ace.msg(result.Msg);
                    me.close();
                    vm.dataTable.reload();
                });
            }
            else
            { 
                model.ids = ids;
                $ace.post("/Wiki/Promotion/Add", model, function (result) {
                    $ace.msg(result.Msg);
                    me.close();
                    vm.loadData();
                });
            }
        }
    }

</script>
